package com.cjy.userReport.action;

import com.base.common.ConfigConstants;
import com.base.common.action.AbstractBaseAction;
import com.base.common.bean.ActionResult;
import com.base.common.enums.TicketStatus;
import com.base.common.persistent.model.Ticket;
import com.base.common.persistent.model.User;
import com.cjy.common.bean.Files;
import com.cjy.common.bean.OpAttributes;
import com.cjy.userReport.service.complaint.ComplaintService;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.apache.struts2.dispatcher.StrutsRequestWrapper;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;

import static com.base.GlobaConstants.*;

@Results({ @Result(name = ASYNCHRONOUS, type = JSON, params = { ROOT,
		ACTION_RESULT_DATA, CONTENT_TYPE, CONTENT_TYPE_JSON }) }) 
public class ComplaintAction extends AbstractBaseAction{

	private static final long serialVersionUID = 1L;
	
	private String address;		// 业主报修物业位置(从省市一直往下)

	private int bid;	// 设备标识符
	
	@Resource
	private ComplaintService complaintService;
  	private String context;		// 业主提交内容
	private int page;			// 页数


	private String picUrls;		// 图片地址,多地址以；分隔
	private String subUserPhone;// 提交人电话
	private int userId;			// 业主物业id(主键ID)
	
	/* Struts2文件上传的三个属性 */
	private File[] filePhotos;
	private String[] filePhotosContentType;
	private String[] filePhotosFileName;
	
	public String getAddress() {
		return address;
	}

	public int getBid() {
		return bid;
	}

	/**
	 * 获取投诉信息列表根据用户和当前页数
	 * 
	 * @return
	 */
	@Action(value = "getComplaintMessagesOfUser")
	public String getComplaintMessagesOfUser() {
		try {
			actionResult = ActionResult.getActionResult(new OpAttributes().add("code", 1).add("result", complaintService.getComplaintMessagesByUser(userId, page)));
		} catch(Exception exception) {
			exception.printStackTrace();
			actionResult = ActionResult.getActionResult(new OpAttributes().add(
					"code", 0));
		}
		return returnType();
	}

	/**
	 * 获取当前用户投诉票据页数
	 * 
	 * @return
	 */
	@Action(value = "getComplaintTicketsPagesOfUser")
	public String getComplaintTicketsPagesOfUser() {
		try {
			Ticket ticket = new Ticket();
			ticket.setCategories(2);
			User user = new User();
			user.setId(userId);
			ticket.setUser(user);
			
			actionResult = ActionResult.getActionResult(new OpAttributes().add(
					"code", 1).add("result", new OpAttributes().add("totalPages", complaintService.getTicketsPagessByUserIdAndCategories(ticket))));
		} catch (Exception e) {
			actionResult = ActionResult.getActionResult(new OpAttributes().add(
					"code", 0));
		}
		return returnType();
	}

	public String getContext() {
		return context;
	}

	public File[] getFilePhotos() {
		return filePhotos;
	}

	public String[] getFilePhotosContentType() {
		return filePhotosContentType;
	}

	public String[] getFilePhotosFileName() {
		return filePhotosFileName;
	}

	public int getPage() {
		return page;
	}

	public String getPicUrls() {
		return picUrls;
	}

	public String getSubUserPhone() {
		return subUserPhone;
	}

	public int getUserId() {
		return userId;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public void setBid(int bid) {
		this.bid = bid;
	}

	public void setContext(String context) {
		this.context = context;
	}

	public void setFilePhotos(File[] filePhotos) {
		this.filePhotos = filePhotos;
	}

	public void setFilePhotosContentType(String[] filePhotosContentType) {
		this.filePhotosContentType = filePhotosContentType;
	}

	public void setFilePhotosFileName(String[] filePhotosFileName) {
		this.filePhotosFileName = filePhotosFileName;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public void setPicUrls(String picUrls) {
		this.picUrls = picUrls;
	}

	public void setSubUserPhone(String subUserPhone) {
		this.subUserPhone = subUserPhone;
	}
	
	public void setUserId(int userId) {
		this.userId = userId;
	}

	/**
	 * 提交投诉信息
	 * 
	 * @return
	 */
	@Action(value = "subComplaintMessage", interceptorRefs = { @InterceptorRef("fileUploadInterceptor") })
	public String subComplaintMessage() {
		try {
			Ticket ticket = new Ticket();
			ticket.setAddress(address);
			ticket.setContext(context);
			ticket.setPicUrls(picUrls);
			ticket.setStatus(TicketStatus.DISPATCH.getValue());
			ticket.setSubUserPhone(subUserPhone);
			User user = new User();
			user.setId(userId);
			ticket.setUser(user);
			
			Files files = new Files();
			files.setFile(filePhotos);
			files.setFileContentType(filePhotosContentType);
			files.setFileFileName(filePhotosFileName);
			// 因为是上传文件，用了common-upload的拦截器处理之后request会报空，要得到一般请求的request头，就要对request做个转型，才能调用相应方法
			HttpServletRequest servletRequest = new StrutsRequestWrapper(request);
			String basePath = null;
			String path = servletRequest.getContextPath();
			basePath = servletRequest.getScheme() + "://"
					+ ConfigConstants.SERVERIP + ":"
					+ servletRequest.getServerPort() + path + "/";
			String result = complaintService.subComplaintMessage(ticket, files, basePath, bid);
			if (result == null) {
				actionResult = ActionResult.getActionResult(new OpAttributes().add(
						"code", 0));
			} else {
				actionResult = ActionResult.getActionResult(new OpAttributes().add(
						"code", 1).add("result", new OpAttributes().add("ticketCode", result)));
			}
		} catch (Exception e) {
			actionResult = ActionResult.getActionResult(new OpAttributes().add(
					"code", 0));
		}
		
		return returnType();
	}
	
}
